﻿(function (wtm) {

    $('body').on('click', '.wtm-mutilupload-del', function () {
        var $el = $(this);
        var $form = $el.parents('form');
        var $container = $el.parents('.wtm-mutilupload-container');
        var $div = $container.parent();
        var fieldId = $div.attr('id').slice(0, -3);
        var fileId = $el.next().val();
        $form.append("<input type='hidden' name ='DeletedfileIds' value = '" + fileId + "'/> ");

        //移除文件容器
        $container.remove();

        //文件数量
        var length = $div.children('.wtm-mutilupload-container').length;
        if (length > 0) {
            $(fieldId).val(length);
        } else {
            $(fieldId).val('');
        }
    });


    wtm.multiupload_Render = function (obj) {
        /* obj.id
         * obj.fileSize
         * obj.ext
         * obj.sysUploadFailed
         * obj.showPreview
         * obj.previewWidth
         * obj.previewHeight
         * obj.cs
         * obj.sysDelete
         * obj.numFileOnce
         * obj.selectedData
         */
        var index = 0;
        var $div = $('#' + obj.id + 'div');

        /* 普通图片上传 */
        layui.upload.render({
            elem: '#' + obj.id + 'button'
            , url: obj.url
            , size: obj.fileSize
            , accept: 'file'
            , multiple: true
            , number: obj.numFileOnce
            , exts: obj.ext
            , before: function (beforeObj) {
                index = layui.layer.load(2);
                obj.previewObj = beforeObj;
            }
            , done: function (res) {
                layui.layer.close(index);
                obj.fileId = res.Data.Id;
                if (res.Data.Id === '') {
                    // 如果文件为空
                    $div.empty();
                    layui.layer.msg(obj.sysUploadFailed);
                }
                else {
                    $('#' + obj.id).val(1);
                    // 是否预览
                    if (obj.showPreview) {
                        var img_index = $div.children('.wtm-mutilupload-img-container').length;

                        var img_tpl = $('#wtmtpl-mutilupload-img').html();
                        var option = {
                            fieldName: obj.fieldName,
                            index: img_index,
                            fileId: res.Data.Id,
                            alt: res.Data.Name,
                            cs: obj.cs,
                            width: obj.previewWidth,
                            height: obj.previewHeight,
                            disabled: obj.disabled
                        };

                        layui.laytpl(img_tpl).render(option, function (str) {
                            $div.append(str);
                        });

                        layui.layer.photos({
                            photos: '#' + obj.id + 'div'
                            , anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                        });

                    } else {
                        var file_index = $div.children('.wtm-mutilupload-file-container').length;
                        var file_tpl = $('#wtmtpl-mutilupload-file').html();
                        var option = {
                            fieldName: obj.fieldName,
                            index: file_index,
                            fileId: res.Data.Id,
                            fileName: res.Data.Name,
                            sysDelete: obj.sysDelete,
                            disabled: obj.disabled
                        };
                        layui.laytpl(file_tpl).render(option, function (str) {
                            $div.append(str);
                        });
                    }
                }
            }
            , error: function () {
                layui.layer.close(index);
            }
        });
    };


    wtm.multiupload_AjaxGet = function (obj) {
        var $div = $('#' + obj.id + 'div');

        obj.allfileids.forEach(function (fileId, index) {
            obj.fileId = fileId;
            var geturl = obj.geturl + "?id=" + fileId + "&_DONOT_USE_CS=" + obj.cs
            $.ajax({
                cache: false,
                type: 'GET',
                url: geturl,
                async: true,
                success: function (data) {//data:文件名
                    $('#' + obj.id).val(1);
                    // 是否预览
                    if (obj.showPreview) {
                        var img_index = $div.children('.wtm-mutilupload-img-container').length;

                        var img_tpl = $('#wtmtpl-mutilupload-img').html();
                        var option = {
                            fieldName: obj.fieldName,
                            index: img_index,
                            fileId: obj, fileId,
                            alt: data,
                            cs: obj.cs,
                            width: obj.previewWidth,
                            height: obj.previewHeight,
                            disabled: obj.disabled
                        };

                        layui.laytpl(img_tpl).render(option, function (str) {
                            $div.append(str);
                        });

                        layui.layer.photos({
                            photos: '#' + obj.id + 'div'
                            , anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                        });
                    } else {
                        var file_index = $div.children('.wtm-mutilupload-file-container').length;
                        var file_tpl = $('#wtmtpl-mutilupload-file').html();
                        var option = {
                            fieldName: obj.fieldName,
                            index: file_index,
                            fileId: obj.fileName,
                            fileName: data,
                            sysDelete: obj.sysDelete,
                            disabled: obj.disabled
                        };
                        layui.laytpl(file_tpl).render(option, function (str) {
                            $div.append(str);
                        });
                    }
                }
            });


        });


    }

})(wtm);